package com.testingedu.action.background;

import java.io.File;

import com.testingedu.jdbcDao.TResourceDao;
import com.testingedu.model.FileInfo;
import com.testingedu.model.TResource;
import com.testingedu.utils.FileUpLoad;

public class TResourceAction extends RequestContext{

	private static final long serialVersionUID = 5000746398762095851L;
	private TResource tResource;
	private static TResourceDao tResourceDao = new TResourceDao(); 
	public void _list() throws Exception {
		String url = request.getParameter("url");
		if ("info".equals(url)) {
			this.info(url);
		} else {
			createQueryResult();
			tResourceDao.list(queryResult, tResource);
			request.setAttribute("queryResult", queryResult);
			request.getRequestDispatcher("/WEB-INF/admin/jsp/resource/list.jsp")
					.forward(request, response);
		}
	}
	
	public void _update() throws Exception {
		String url = request.getParameter("url");
		if ("update".equals(url)) {
			request.setAttribute("tResource", tResourceDao.info(tResource.getId()));
			request.getRequestDispatcher("/WEB-INF/admin/jsp/resource/update.jsp")
					.forward(request, response);
		} else {
				tResourceDao.update(tResource);
				response.sendRedirect("/testingedu2/admin/resource/list");
		}
	}
	
	public void _add() throws Exception {
		String url = request.getParameter("url");
		if ("jsp".equals(url)) {
			request.getRequestDispatcher("/WEB-INF/admin/jsp/resource/add.jsp")
					.forward(request, response);
		} else {
			FileInfo uploadFile = new FileInfo();
			if(tResource == null) {
				tResource = new TResource();
			}
			try {
				String path = FileUpLoad.execute(request, response, servletContext, tResource, uploadFile, "WEB-INF"+File.separator +"resources");
				if(path != null && path.trim().length() > 0){
					tResource.setPath(path);
				}
				tResourceDao.add(tResource);
				response.sendRedirect("/testingedu2/admin/resource/list");
			} catch (Exception e) {
				e.printStackTrace();
				request.getRequestDispatcher("/error/error.jsp")
				.forward(request, response);
			}
		}
	}
	
	public void settResource(TResource tResource) {
		this.tResource = tResource;
	}

	public void _delete() throws Exception {
		tResource = tResourceDao.info(tResource.getId());
		String path = servletContext.getRealPath(File.separator) + tResource.getPath();
		if('/' == File.separatorChar)
			path = path.replace("\\", File.separator);
		if('\\' == File.separatorChar)
			path = path.replace("/", File.separator);
		File dFile = new File(path);
		if(dFile.exists())
			dFile.delete();
		tResourceDao.delete(tResource.getId());
		response.sendRedirect("/testingedu2/admin/resource/list");
	}
	
	public static void main(String[] args) {
		String str = "/fdsffd/";
		System.out.println(str.replace("/", File.separator));
	}


	private void info(String url) throws Exception {
		tResource = tResourceDao.info(tResource.getId());
		request.setAttribute("tResource", tResource);
		request.getRequestDispatcher(
				"/WEB-INF/admin/jsp/resource/" + url + ".jsp").forward(
				request, response);
	}
}
